package edu.hawaii.myisern.brown.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import com.meterware.httpunit.TableCell;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

/**
 * Tests operation of the MyIsern researcher web pages.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class TestResearcherActionBeans {

  /** Get the test host. */
  private String testHost = (System.getProperty("test.host") == null) ? "http://localhost:8080/"
      : System.getProperty("test.host");

  /** User name to use. */
  private String username = "test";

  /** Password name to use. */
  private String password = "test";

  /**
   * Test the main menu new researcher button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuNewResearcherButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login",
        response.getTitle());

    // Push login button
    WebForm form = response.getFormWithID("LoginForm");
    WebRequest request = form.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    WebResponse mainPage = response = conversation.getResponse(form
        .getRequest());
    assertEquals("Checking mainMenu.jsp retrieval",
        "MyIsern - Main Menu", response.getTitle());

    // Click on new researcher link.
    WebLink link = response.getLinkWithID("NewResearcherLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Edit Researcher",
        response.getTitle());

    // Click on cancel.
    form = response.getFormWithID("EditResearcherForm");
    response = conversation.getResponse(form.getRequest("cancel"));
    assertEquals("Checking mainMenu.jsp retrieval",
        "MyIsern - Main Menu", response.getTitle());

    // Click on new researcher link.
    link = response.getLinkWithID("NewResearcherLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result", "MyIsern - Edit Researcher",
        response.getTitle());

    // Click on save.
    form = response.getFormWithID("EditResearcherForm");
    form.setParameter("name", "Test Researcher");
    form.setParameter("email", "Test Email");
    form.setParameter("organization", "University of Hawaii");
    form.setParameter("pictureLink", "Test Picture Link");
    form.setParameter("bioStatement", "Test Bio Statement");
    response = conversation.getResponse(form.getRequest("save"));
    assertEquals("Checking mainMenu.jsp retrieval",
        "MyIsern - Edit Researcher", response.getTitle());

    // Click on edit researcher link, brining up select list.
    response = mainPage;
    link = response.getLinkWithID("EditResearcherLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking result", "MyIsern - Select Researcher",
        response.getTitle());

    // Check if new name is in select list and click it.
    WebTable table = response.getTableWithID("SelectResearcherTable");
    TableCell cell = table.getTableCellWithID("Test Researcher");
    assertNotNull("Check if cell exisits", cell);
    link = cell.getLinkWith("Test");
    assertNotNull("Check if cell exisits", link);

    // Check that we are back at Edit screen, and check the fields for
    // data.
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking result", "MyIsern - Edit Researcher",
        response.getTitle());
    form = response.getFormWithID("EditResearcherForm");
    assertEquals("Check field.", "Test Researcher", form
        .getParameterValue("name"));
    assertEquals("Check field.", "Test Email", form
        .getParameterValue("email"));
    assertEquals("Check field.", "University of Hawaii", form
        .getParameterValue("organization"));
    assertEquals("Check field.", "Test Picture Link", form
        .getParameterValue("pictureLink"));
    assertEquals("Check field.", "Test Bio Statement", form
        .getParameterValue("bioStatement"));
  }
}
